﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZF.BLL;
using System.Linq.Expressions;
using GCELibrary.Current;
using ZF.BLL.Page;
using System.Linq.Dynamic;
using System.Linq.Expressions;
using ZF.BLL.Linq;
using ZF.BLL;
namespace ZF.WS
{
 
    public class News : PageBase
    {
         
        public virtual string ListPage { get { return string.Format("{0}/News/NewsList.aspx", AppKey.SiteKey.ManageDirectory); } }
        public virtual string ManagePage { get { return string.Format("{0}/News/NewsManage.aspx", AppKey.SiteKey.ManageDirectory); } }
        public virtual string CTitle { get { return "新闻资讯"; } }
    }
    public partial class WebForm2 : News
    {

        [PageArgument(Key = "t", DefaultValue = 0)]
        public int Type { get; set; }
        [PageArgument(Key = "pt", DefaultValue = 0)]//0为新闻，1为网站投稿
        public int pt { get; set; }



        public string[] Types { get { return DictionaryConfig.INewType.Select(p => p.Key).ToArray(); } }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                InitControls();
                View();
            }
        }
        public void InitControls()
        {

        }
        public void View()
        {
            ListBind();
        }
        /// <summary>
        /// 列表绑定
        /// </summary>
        public void ListBind()
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {

                List<Expression<Func<Model.I_News, bool>>> filterList = new List<Expression<Func<Model.I_News, bool>>>();
                IQueryable<Model.I_News> selector = dc.I_News;
                if (this.pt == 0)
                    filterList.Add(p => p.IsV);
                else
                    filterList.Add(p => !p.IsV);

                if (this.Type > 0)
                {
                    filterList.Add(p => p.Type == Types[this.Type - 1]);
                }
                foreach (var filter in filterList)
                {
                    selector = selector.Where(filter);
                }
                var selWhere = selector.Select(p =>
                    new
                    {
                        News = p,
                    }).Where(this.gvList);
                var selPage = selWhere.OrderBy(this.gvList.SortExpression)
                        .Skip(gvList.PageIndex * gvList.PageSize).Take(gvList.PageSize);
                this.gvList.RowCount = selWhere.Count();
                var source = selPage.ToList();
                 
                this.gvList.DataSource = source;
                this.gvList.DataBind();

            }
        }
        /// <summary>
        /// 排序事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void gvListSorting(object sender, GridViewSortEventArgs e)
        {
            ListBind();
        }
        /// <summary>
        /// 换页事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void gvListPageChanging(object sender, GridViewPageEventArgs e)
        {
            this.gvList.PageIndex = e.NewPageIndex;
            ListBind();
        }
        public void Delete(params int[] idArr)
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {
                bool isSuccess = true;
                try
                {
                    dc.I_News.DeleteAllOnSubmit(
                       dc.I_News.Where(p => idArr.Contains(p.NewsId)));
                    dc.SubmitChanges();
                }
                catch (Exception ex)
                {
                    isSuccess = false;
                    throw SiteInfo.WarnFactory.CreateWarn(ex);
                }
                string msgKey = "新闻资讯";
                if (isSuccess)
                    WarnFactory.CreateWarn(string.Format("删除{0}成功", msgKey), WarnIcon.Success, WarnLink.Current).Alert();

            }
        }
        public void Delete(object sender, EventArgs e)
        {
            var bc = (IButtonControl)sender;
            if (bc.CommandArgument == "select")
            {
                var idArrTemp = this.gvList.GetCheckKeys();
                if (idArrTemp.Length == 0)
                    throw SiteInfo.WarnFactory.CreateWarn("未选择任何记录");
                var idArr = idArrTemp.Select(p => p.Convert<int>()).ToArray();
                Delete(idArr);
            }
            else
            {
                Delete(bc.CommandArgument.Convert<int>());
            }
        }
        public void CY(object sender, EventArgs e)
        {
            var bc = (IButtonControl)sender;
            var id = bc.CommandArgument.Convert<int>();
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {
                bool isSuccess = true;
                try
                {
                    dc.I_News.Where(p => id == p.NewsId).ToList().ForEach(p => p.IsV = true);

                    dc.SubmitChanges();
                }
                catch (Exception ex)
                {
                    isSuccess = false;
                    throw SiteInfo.WarnFactory.CreateWarn(ex);
                }
                WarnFactory.CreateWarn(new PageWarnParameter()
                {
                    Link = WarnLink.Current,
                    Message = "操作成功",
                    JscriptMode = true,
                }).Alert();

            }
        }
        public void Search(object sender, EventArgs e)
        {
            //this.gvList.RemoveAllFilterExpression();
            //if (this.tbTitle.Text.EmptyString() != null)
            //    this.gvList.AddFilterExpression("House.Title.Contains(@0)", this.tbTitle.Text);
            //if (this.tbHouseNo.Text.EmptyString() != null)
            //    this.gvList.AddFilterExpression("House.HouseId=@0", this.tbHouseNo.Text.Convert<int>()-10000);
            //if (this.ddlHouseType.SelectedValue != "-1")
            //    this.gvList.AddFilterExpression("House.HouseType=@0", this.ddlHouseType.SelectedValue);
            //if (this.ddlHouseStatus.SelectedValue != "-1")
            //    this.gvList.AddFilterExpression("House.Status=@0", this.ddlHouseStatus.SelectedValue.Convert<int>());
            //if (this.tbEndTime1.Text.EmptyString() != null)
            //    this.gvList.AddFilterExpression("House.AddTime<=@0", this.tbEndTime1.Text.Convert<DateTime>());
            //if (this.tbEndTime2.Text.EmptyString() != null)
            //    this.gvList.AddFilterExpression("House.AddTime>=@0", this.tbEndTime2.Text.Convert<DateTime>());

            ListBind();
        }
        public void AllList(object sender, EventArgs e)
        {
            this.gvList.RemoveAllFilterExpression();
            ListBind();
        }

    }
}